﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;
using Quanlybanhang_pro202.Control;

namespace Quanlybanhang_pro202.View
{
    public partial class FrmAdmin : Form
    {
        public FrmAdmin()
        {
            InitializeComponent();
        }

        private void FrmAdmin_Load(object sender, EventArgs e)
        {
            LoadHd();
            LoadCBBThuNgan();
            LoadKhachhang();
            LoadNhanVien();

        }

        private void LoadHd()
        {
            Hoadon_control hd = new Hoadon_control();
            dtgHoaDon.DataSource = hd.GetAllInvoices().Tables["hoadon"];
        }

        
        private void LoadCBBThuNgan()
        {
            Taikhoan_Control tk = new Taikhoan_Control();
            cbbThuNganHD.DisplayMember = "tendangnhap";
            cbbThuNganHD.DataSource = tk.GetMember().Tables["thungan"];
        }
        private void GetMameUser()
        {
            Taikhoan_Control tk = new Taikhoan_Control();
            DataTable dt = tk.GetMember().Tables["thungan"];
            foreach (DataRow dong in dt.Rows)
            { 
            if (cbbThuNganHD.Text == dong[1].ToString())
            {
            txtTenThuNgan.Text = dong[4].ToString();
            }
            }
        }
        private void LoadKhachhang()
        {
            Khachhang_Control kh = new Khachhang_Control();
            dtgKhachhang.DataSource = kh.GetAllCustome().Tables["khachhang"];
            cbbMaKHHD.DataSource = kh.GetAllCustome().Tables["khachhang"];
            cbbMaKHHD.DisplayMember = "makh";
        }
        private void LoadTenKH()
        {
            Khachhang_Control kh = new Khachhang_Control();
            DataTable dt = kh.GetAllCustome().Tables["khachhang"];
            foreach (DataRow dong in dt.Rows)
            {
                if (cbbMaKHHD.Text == dong[0].ToString()) 
                {
                    txtTenKhHD.Text = dong[1].ToString();
                }
            }

        }
        private void LoadNhanVien()
        {
            Taikhoan_Control tk = new Taikhoan_Control();
            dtgNhanvien.DataSource = tk.GetAllUser().Tables["taikhoan"];
        }
        private void dtgHoaDon_Click(object sender, EventArgs e)
        {
            txtMaHD.Enabled = true;
            txtNgayHD.Enabled = true;
            cbbMaKHHD.Enabled = true;
            cbbThuNganHD.Enabled = true;
            txtMaHD.Text = dtgHoaDon.CurrentRow.Cells[0].Value.ToString();
            txtNgayHD.Text = dtgHoaDon.CurrentRow.Cells[3].Value.ToString().Substring(0,10);
            cbbThuNganHD.Text = dtgHoaDon.CurrentRow.Cells[1].Value.ToString();
            cbbMaKHHD.Text = dtgHoaDon.CurrentRow.Cells[2].Value.ToString();
        }

        private void cbbThuNganHD_SelectedIndexChanged(object sender, EventArgs e)
        {
            GetMameUser();
        }

        private void cbbMaKHHD_SelectedIndexChanged(object sender, EventArgs e)
        {
            LoadTenKH();
        }

        private void btnLuuKH_Click(object sender, EventArgs e)
        {
            try
            {
                Khachhang_Control kh = new Khachhang_Control();
                int i = kh.InsertCustomer(txtTenKhHD.Text,txtDiaChiKH.Text,txtSDTKH.Text);
                if (i > 0)
                {
                    MessageBox.Show("Insert success!");
                }
            }
            catch
            {
                MessageBox.Show("Fail to Insert new customer! please try again");
            }
        }

        

       
    }
}
